package org.stream.answerduel.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.stream.answerduel.dto.Result;
import org.stream.answerduel.service.MQService;
import org.stream.answerduel.service.RobotService;
import org.stream.answerduel.vo.Message.*;
import org.stream.answerduel.vo.Message.EnumType.ResultType;
import org.stream.answerduel.vo.RobotVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @Auther: 苏建誉
 * @Date: 2019/7/10 11:38
 */
@Slf4j
@Api("消息队列接口")
@RestController
@RequestMapping("/api/MQ")
public class TestController {
    @Autowired
    private MQService service;

    @Autowired
    private RobotService robotService;

    //@Autowired
    //private RobotService robotService;
    /**
     * 功能描述：
     *
     * @param content
     * @param topic
     * @return
     */
    @ApiOperation(value = "发布", notes = "发布消息")
    @ApiImplicitParams({
    })
    @PostMapping("/publish")
    public ResponseEntity<Result> publish(String content,String topic) {
        try{
            service.publish(content,topic);
        }catch(MqttException e){
            return new ResponseEntity(new Result().serverError("publish message fail"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(new Result().ok("publish message success"), HttpStatus.OK);
    }

    /**
     * 功能描述：
     *
     * @param topic
     * @return
     */
    @ApiOperation(value = "订阅", notes = "订阅消息")
    @ApiImplicitParams({
    })
    @PostMapping("/subscribe")
    public ResponseEntity<Result> subscribe(String topic) {
        try{
            service.subscribe(topic);
        }catch(MqttException e){
            return new ResponseEntity(new Result().serverError("subscribe message fail"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(new Result().ok("subscribe message success"), HttpStatus.OK);
    }

    /**
     * 功能描述：对象序列化字符串测试
     *
     *
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询消息")
    @ApiImplicitParams({
    })
    @GetMapping("/ObjectSerializer")
    public String ObjectSerializer (){
        List<String> list=new ArrayList<String>(Arrays.asList("A:苹果", "B:香蕉", "C:橘子","D:西瓜"));
        FriendsApplyMessage message=new FriendsApplyMessage(1L,"我");
        AnswerMessage message1=new AnswerMessage(10,5);
        RobotVo robot=robotService.createRobot(RobotService.Grade.difficulty);

        PvpMessage message2=new PvpMessage(robot,1L);
        SubjectMessage message3=new SubjectMessage("下列是水果的是:",list);
        ResultMessage message4=new ResultMessage(ResultType.win,5,5,0);
        //RoomParticipationMessage message5=new RoomParticipationMessage(robot,1L,false);
        QuitRoomMessage message6=new QuitRoomMessage(123L);
        String str= message.toString();
        String str1=message1.toString();
        String str2=message2.toString();
        String str3=message3.toString();
        String str4=message4.toString();
        //String str5=message5.toString();
        String str6=message6.toString();
        return "好友申请:"+str+"答题情况:"+str1+"匹配对手信息:"+str2+"题目信息:"+str3+"最终对战信息:"+str4+"玩家退出信息:"+str6;
    }

    /**
     * 测试重定向
     *
     */
    @GetMapping("redirect")
    @ResponseBody
    public void redirect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        HttpSession seesion = httpServletRequest.getSession();
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/api/user/tokenOverdue");
    }

}
